package com.hh.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hh.demo.entity.Rent;
import com.hh.demo.service.IRentService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 吉吉国王
 * @since 2022-12-05
 */
@Controller
public class RentController {
    @Resource
    private IRentService rentService;
    Date date = new Date();
    SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");

    @PostMapping("/rentToy")
    public void rentToy(String toyId,String userId){
        Rent rent = new Rent();
        rent.setUserId(userId);
        rent.setToyId(toyId);
        rent.setRentDate(format.format(date));
        rent.setState("已借出");
        rentService.save(rent);
    }
    @PostMapping("/returnToy")
    @ResponseBody
    public void returnToy(String toyId,String userId){
        //根据玩具id和用户id查询数据
        String  format= this.format.format(date);
        rentService.returnToy(toyId,userId,format);
    }


    @RequestMapping("/queryCount")
    @ResponseBody
    public List<Rent> queryCount(){
        return rentService.queryCount();
    }
}
